/**
 *                            License
 * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS  
 * CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). 
 * THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW.  
 * ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR  
 * COPYRIGHT LAW IS PROHIBITED.
 * 
 * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND  
 * AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE  
 * MAY BE CONSIDERED TO BE A CONTRACT,
 * THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED 
 * HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
 * 
 */
package com.lineage.server.model.item.etcitem.action;

import static com.lineage.server.model.skill.L1SkillId.ABSOLUTE_BARRIER;
import static com.lineage.server.model.skill.L1SkillId.EARTH_BIND;
import static com.lineage.server.model.skill.L1SkillId.ICE_LANCE;

import com.lineage.server.codes.ActionCodes;
import com.lineage.server.model.L1Object;
import com.lineage.server.model.L1PcInventory;
import com.lineage.server.model.L1World;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1MonsterInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.item.ItemExecutor;
import com.lineage.server.packetserver.S_UseAttackSkill;
import com.lineage.server.utils.Random;

/**
 * Class <code>WandLightning</code> 闪电魔杖 - 40007.
 * 
 * @author jrwz
 * @version 2012-6-19下午13:30:04
 * @see com.lineage.server.model.item.etcitem.action
 * @since JDK1.7
 */
public final class WandLightning extends ItemExecutor {

    /**
     * 取得实例.
     * 
     * @return 实例
     */
    public static ItemExecutor get() {
        return new WandLightning();
    }

    /** 闪电魔杖 - 40007. */
    private WandLightning() {
    }

    /**
     * 魔杖动作.
     * 
     * @param user
     *            -使用者
     * @param target
     *            -目标
     * @return 伤害值
     */
    private int doWandAction(final L1PcInstance user, final L1Object target) {
        if (user.getId() == target.getId()) {
            return 0; // 目标为自身
        }
        if (!user.glanceCheck(target.getX(), target.getY())) {
            return 0; // 有障碍物
        }

        // 伤害值计算公式
        int dmg = (Random.nextInt(11) - 5) + user.getStr();
        dmg = Math.max(1, dmg);

        if (target instanceof L1PcInstance) {
            final L1PcInstance pc = (L1PcInstance) target;
            if (pc.getMap().isSafetyZone(pc.getLocation())
                    || user.checkNonPvP(user, pc)) {
                return 0;
            }
            if ((pc.hasSkillEffect(ICE_LANCE))
                    || (pc.hasSkillEffect(ABSOLUTE_BARRIER))
                    || (pc.hasSkillEffect(EARTH_BIND))) {
                return 0;
            }

            final int newHp = pc.getCurrentHp() - dmg;
            if (newHp > 0) {
                pc.setCurrentHp(newHp);
            } else if ((newHp <= 0) && pc.isGm()) {
                pc.setCurrentHp(pc.getMaxHp());
            } else if ((newHp <= 0) && !pc.isGm()) {
                pc.death(user);
            }
            return dmg;
        } else if (target instanceof L1MonsterInstance) {
            final L1MonsterInstance mob = (L1MonsterInstance) target;
            mob.receiveDamage(user, dmg);
            return dmg;
        }
        return 0;
    }

    @Override
    public void execute(final L1PcInstance pc, final L1ItemInstance useItem,
            final int readC1, final int readC2, final int targetId,
            final int targetX, final int targetY) {
        final int spellscObjid = targetId;
        final int spellscX = targetX;
        final int spellscY = targetY;

        int dmg = 0;
        int[] data1 = null;
        final L1Object target = L1World.getInstance().findObject(spellscObjid);
        if (target != null) {
            dmg = this.doWandAction(pc, target);
        }
        data1 = new int[] { ActionCodes.ACTION_WAND, dmg, 10, 6 }; // data =
                                                                   // {actid,
                                                                   // dmg,
                                                                   // spellgfx,
                                                                   // use_type}
        pc.sendPackets(new S_UseAttackSkill(pc, spellscObjid, spellscX,
                spellscY, data1));
        pc.broadcastPacket(new S_UseAttackSkill(pc, spellscObjid, spellscX,
                spellscY, data1));
        useItem.setChargeCount(useItem.getChargeCount() - 1);
        pc.getInventory().updateItem(useItem, L1PcInventory.COL_CHARGE_COUNT);
        if (useItem.getChargeCount() <= 0) { // 次数为 0时删除
            pc.getInventory().removeItem(useItem, 1);
        }
    }
}
